// Attempt to use chrome types directly if browser namespace resolution fails.
// This requires chrome types to be available in your project (WXT usually handles this).

/**
 * Retrieves a specific cookie by name and URL using chrome.cookies API.
 *
 * @param name The name of the cookie to retrieve.
 * @param url The URL associated with the cookie.
 * @returns A Promise that resolves to the chrome.cookies.Cookie object if found, otherwise null.
 */
export async function getCookie(name: string, url: string): Promise<chrome.cookies.Cookie | null> {
  // Check if chrome.cookies is available (it should be in an extension environment)
  if (typeof chrome !== 'undefined' && chrome.cookies) {
    try {
      const cookieDetails: chrome.cookies.GetDetails = { name, url };
      // Using a callback version for chrome.cookies.get as it might not always return a Promise directly
      // depending on the environment/polyfill version if browser.* was intended.
      return new Promise((resolve) => {
        chrome.cookies.get(cookieDetails, (cookie) => {
          if (chrome.runtime.lastError) {
            console.error(`Error getting cookie "${name}" for URL "${url}":`, chrome.runtime.lastError.message);
            resolve(null);
          } else {
            resolve(cookie);
          }
        });
      });
    } catch (error) {
      console.error(`Exception when trying to get cookie "${name}" for URL "${url}":`, error);
      return null;
    }
  } else {
    console.error("chrome.cookies API is not available.");
    return null;
  }
}

/**
 * Retrieves all cookies matching the given details using chrome.cookies API.
 *
 * @param details An object containing details to filter the cookies by (chrome.cookies.GetAllDetails).
 * @returns A Promise that resolves to an array of chrome.cookies.Cookie objects.
 */
export async function getAllCookies(details: chrome.cookies.GetAllDetails): Promise<chrome.cookies.Cookie[]> {
  if (typeof chrome !== 'undefined' && chrome.cookies) {
    try {
      return new Promise((resolve) => {
        chrome.cookies.getAll(details, (cookies) => {
          if (chrome.runtime.lastError) {
            console.error('Error getting all cookies with details:', details, chrome.runtime.lastError.message);
            resolve([]);
          } else {
            resolve(cookies || []); // Ensure an array is resolved even if null/undefined comes back
          }
        });
      });
    } catch (error) {
      console.error('Exception when trying to get all cookies with details:', details, error);
      return [];
    }
  } else {
    console.error("chrome.cookies API is not available.");
    return [];
  }
}

// Example usage (ensure extension is loaded with cookies permission):
/*
async function testCookieFunctions() {
  const targetUrl = "https://www.xiaohongshu.com";
  const specificCookieName = "web_session";

  console.log('[Using chrome.cookies] Attempting to get cookie:', specificCookieName, targetUrl);
  const specificCookie = await getCookie(specificCookieName, targetUrl);
  if (specificCookie) {
    console.log('Found specific cookie (chrome.cookies):', specificCookie);
  } else {
    console.log(`Cookie "${specificCookieName}" not found (chrome.cookies).`);
  }

  console.log('[Using chrome.cookies] Attempting to get all cookies for url:', targetUrl);
  const allSiteCookies = await getAllCookies({ url: targetUrl });
  if (allSiteCookies.length > 0) {
    console.log(`Found ${allSiteCookies.length} cookies for ${targetUrl} (chrome.cookies):`);
    allSiteCookies.forEach(cookie => console.log(`- ${cookie.name}`));
  } else {
    console.log(`No cookies found for ${targetUrl} (chrome.cookies).`);
  }
}
// testCookieFunctions();
*/ 